

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;
import javax.swing.table.DefaultTableModel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.ContainerEvent;
import java.awt.event.ContainerListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.UIManager;
import javax.swing.border.EtchedBorder;

public class DeptModifyPanel extends JPanel {
	private JPanel panel;
	private JLabel lblNewLabel;
	private JPanel panel_1;
	private JLabel lblNewLabel_1;
	private JPanel panel_2;
	private JPanel panel_3;
	private JButton btnNewButton_2;
	private JButton btnNewButton_3;
	private JButton btnNewButton_4;
	private JComboBox cbSearchDept;
	private JTextField tfDeptInfo;
	private JButton btnSearch;
	private JScrollPane scrollPane;
	private JTable table;
	private JSplitPane splitPane;
	private JButton btnDelete;
	private JButton btnModify;
	private JLabel label;
	private JLabel label_1;
	private JLabel label_2;
	private JTextField tfDeptId;
	private JTextField tfDeptNm;
	private JLabel label_3;
	private JLabel label_4;
	private JLabel label_5;
	private JTextField tfMnageId;
	private JTextField tfLcId;
	
	private Connection conn 			= null;
	private ResultSet result 			= null;
	private PreparedStatement pstmt 	= null;
	private DefaultTableModel dtm 		= null;
	private Vector<MemberBean> vector 	= null;
	
	private int listSelect 				= 0;
	
	private String selDeptId			= "";
	
	/**
	 * Create the panel.
	 */
	public DeptModifyPanel() {
		setBackground(new Color(173, 216, 230));
		setLayout(new BorderLayout(0, 0));
		add(getSplitPane(), BorderLayout.SOUTH);
		add(getPanel(), BorderLayout.NORTH);
		add(getPanel_1(), BorderLayout.WEST);
		add(getPanel_2(), BorderLayout.CENTER);
		
		
	}
	
	private void connect() throws SQLException, ClassNotFoundException
	{
		conn = DBconn.getConnection();
	}
	
	private void close() throws SQLException
	{
		DBconn.close();
		conn = null;
	}
	
	private JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.setBackground(new Color(245, 255, 250));
			panel.setPreferredSize(new Dimension(10, 34));
			panel.setLayout(new BorderLayout(0, 0));
			panel.add(getLabel_1(), BorderLayout.WEST);
			panel.add(getPanel_3(), BorderLayout.CENTER);
		}
		return panel;
	}
	private JLabel getLabel_1() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel();
			lblNewLabel.setBackground(new Color(245, 255, 250));
			lblNewLabel.setText("수정 & 삭제");
			lblNewLabel.setForeground(new Color(0, 0, 0));
			lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
			lblNewLabel.setFont(new Font("돋움", Font.BOLD, 17));
			lblNewLabel.setPreferredSize(new Dimension(120, 15));
		}
		return lblNewLabel;
	}
	private JPanel getPanel_1() {
		if (panel_1 == null) {
			panel_1 = new JPanel();
			panel_1.setBackground(new Color(245, 255, 250));
			panel_1.setPreferredSize(new Dimension(120, 10));
			panel_1.setMinimumSize(new Dimension(30, 30));
			panel_1.setLayout(null);
			panel_1.add(getLabel_2());
			panel_1.add(getLabel_2_1());
			panel_1.add(getLabel());
			panel_1.add(getLabel_1_1());
		}
		return panel_1;
	}
	private JLabel getLabel_2() {
		if (lblNewLabel_1 == null) {
			lblNewLabel_1 = new JLabel(); 
			lblNewLabel_1.setText("부서의 ");
			lblNewLabel_1.setFont(new Font("굴림", Font.BOLD, 16));
			lblNewLabel_1.setBounds(39, 10, 69, 26);
		}
		return lblNewLabel_1;
	}
	private JPanel getPanel_2() {
		if (panel_2 == null) {
			panel_2 = new JPanel();
			panel_2.setBackground(new Color(245, 255, 250));
			panel_2.setLayout(null);
			panel_2.add(getCbSearchDept());
			panel_2.add(getTfDeptInfo());
			panel_2.add(getBtnSearch());
			panel_2.add(getScrollPane());
			panel_2.add(getTfDeptId());
			panel_2.add(getTfDeptNm());
			panel_2.add(getLabel_3());
			panel_2.add(getLabel_4());
			panel_2.add(getLabel_5());
			panel_2.add(getTfMnageId());
			panel_2.add(getTfLcId());
		}
		return panel_2;
	}
	private JPanel getPanel_3() {
		if (panel_3 == null) {
			panel_3 = new JPanel();
			panel_3.setBackground(new Color(245, 255, 250));
			panel_3.setLayout(new BorderLayout(0, 0));
			panel_3.add(getBtnNewButton_2(), BorderLayout.EAST);
			panel_3.add(getBtnNewButton_3(), BorderLayout.CENTER);
			panel_3.add(getBtnNewButton_4(), BorderLayout.WEST);
		}
		return panel_3;
	}
	private JButton getBtnNewButton_2() {
		if (btnNewButton_2 == null) {
			btnNewButton_2 = new JButton();
			btnNewButton_2.setForeground(new Color(255, 255, 255));
			btnNewButton_2.setFont(new Font("Dialog", Font.BOLD, 17));
			btnNewButton_2.setBackground(new Color(135, 206, 250));
			btnNewButton_2.setPreferredSize(new Dimension(160, 9));
			btnNewButton_2.setMinimumSize(new Dimension(160, 9));
			btnNewButton_2.setMaximumSize(new Dimension(160, 9));
			btnNewButton_2.setText("수정 & 삭제");
		}
		return btnNewButton_2;
	}
	private JButton getBtnNewButton_3() {
		if (btnNewButton_3 == null) {
			btnNewButton_3 = new JButton(); 
			btnNewButton_3.setForeground(new Color(255, 255, 255));
			btnNewButton_3.setFont(new Font("Dialog", Font.BOLD, 17));
			btnNewButton_3.setBackground(new Color(135, 206, 250));
			btnNewButton_3.setText("부서 등록");
			btnNewButton_3.setMinimumSize(new Dimension(120, 23));
			btnNewButton_3.setMaximumSize(new Dimension(120, 23));
			btnNewButton_3.setPreferredSize(new Dimension(120, 23));
		}
		return btnNewButton_3;
	}
	private JButton getBtnNewButton_4() {
		if (btnNewButton_4 == null) {
			btnNewButton_4 = new JButton(); 
			btnNewButton_4.setForeground(new Color(255, 255, 255));
			btnNewButton_4.setFont(new Font("Dialog", Font.BOLD, 17));
			btnNewButton_4.setBackground(new Color(135, 206, 250));
			btnNewButton_4.setText("부서 조회");
			btnNewButton_4.setPreferredSize(new Dimension(160, 23));
			btnNewButton_4.setMinimumSize(new Dimension(120, 23));
			btnNewButton_4.setMaximumSize(new Dimension(120, 23));
		}
		return btnNewButton_4;
	}
	

	private JComboBox getCbSearchDept() {
		if (cbSearchDept == null) {
			cbSearchDept = new JComboBox();
			cbSearchDept.setFont(new Font("굴림", Font.BOLD, 14));
			cbSearchDept.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, new Color(70, 130, 180)));
			cbSearchDept.setBackground(UIManager.getColor("Button.light"));
			cbSearchDept.setModel(new DefaultComboBoxModel(new String[] {"ID", "Name"}));
			cbSearchDept.setBounds(12, 10, 88, 27);
		}
		return cbSearchDept;
	}
	
	private JTextField getTfDeptInfo() {
		if (tfDeptInfo == null) {
			tfDeptInfo = new JTextField();
			tfDeptInfo.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					deptSelect();
				}
			});
			tfDeptInfo.setColumns(10);
			tfDeptInfo.setBounds(112, 10, 273, 27);
		}
		return tfDeptInfo;
	}
	
	private JButton getBtnSearch() {
		if (btnSearch == null) {
			btnSearch = new JButton("Search");
			btnSearch.setFont(new Font("굴림", Font.BOLD, 14));
			btnSearch.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, new Color(70, 130, 180)));
			btnSearch.setBackground(UIManager.getColor("Button.light"));
			btnSearch.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					deptSelect();
				}
			});
			btnSearch.setBounds(397, 10, 105, 27);
		}
		return btnSearch;
	}
	
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setBounds(12, 47, 490, 170);
			scrollPane.setViewportView(getTable());
		}
		return scrollPane;
	}
	
	private JTable getTable() {
		if (table == null) {
			table = new JTable();
			table.addMouseListener(new MouseListener() {
				@Override
				public void mouseReleased(MouseEvent arg0) {}
				@Override
				public void mousePressed(MouseEvent arg0) {}
				@Override
				public void mouseExited(MouseEvent arg0) {}
				@Override
				public void mouseEntered(MouseEvent arg0) {}
				@Override
				public void mouseClicked(MouseEvent arg0) {
					listSelect = table.getSelectedRow();
					
					for( int i = 0; i < vector.size(); i++ ) {
						if( listSelect == i ) {
							tfDeptId.setText("");
							tfDeptNm.setText("");
							tfMnageId.setText("");
							tfLcId.setText("");
							
							tfDeptId.setText(String.valueOf(vector.get(i).getDeptId()));
							tfDeptNm.setText(vector.get(i).getDeptName());
							tfMnageId.setText(String.valueOf(vector.get(i).getMngId()));
							tfLcId.setText(String.valueOf(vector.get(i).getLocationId()));
							
							selDeptId = String.valueOf(vector.get(i).getDeptId());
							
							break;
						}
					}
				}
			});
			
			deptSelect();
		}
		return table;
	}
	
	private JSplitPane getSplitPane() {
		if (splitPane == null) {
			splitPane = new JSplitPane();
			splitPane.setBackground(new Color(245, 255, 250));
			splitPane.setLeftComponent(getBtnDelete());
			splitPane.setRightComponent(getBtnModify());
		}
		return splitPane;
	}
	
	private JButton getBtnDelete() {
		if (btnDelete == null) {
			btnDelete = new JButton();
			btnDelete.setText("삭제");
			btnDelete.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					int yn = JOptionPane.showConfirmDialog( DeptModifyPanel.this, 
							"선택하신 부서를 삭제 하시겠습니까?");
					if( yn == JOptionPane.OK_OPTION ) {
						deptUpdate(); // 해당 부서에 속한 사원의 dept_ID를 0(대기발령)으로 변경
						deptDelete(); // 해당 부서를 삭제
						deptSelect(); // 부서 재조회
					}
				}
			});
			btnDelete.setMinimumSize(new Dimension(120, 23));
			btnDelete.setMaximumSize(new Dimension(120, 23));
		}
		return btnDelete;
	}
	
	private JButton getBtnModify() {
		if (btnModify == null) {
			btnModify = new JButton();
			btnModify.setText("수정");
			btnModify.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					deptUpdate();
				}
			});
		}
		return btnModify;
	}
	
	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel("Department ID : ");
			label.setBounds(29, 277, 96, 15);
		}
		return label;
	}
	
	private JLabel getLabel_1_1() {
		if (label_1 == null) {
			label_1 = new JLabel("Department Name :");
			label_1.setBounds(5, 307, 121, 15);
		}
		return label_1;
	}
	
	private JLabel getLabel_2_1() {
		if (label_2 == null) {
			label_2 = new JLabel("* 필수 입력 *");
			label_2.setFont(new Font("굴림", Font.BOLD, 14));
			label_2.setBounds(16, 250, 96, 15);
		}
		return label_2;
	}
	
	private JTextField getTfDeptId() {
		if (tfDeptId == null) {
			tfDeptId = new JTextField();
			tfDeptId.setColumns(10);
			tfDeptId.setBounds(14, 273, 116, 21);
		}
		return tfDeptId;
	}
	
	private JTextField getTfDeptNm() {
		if (tfDeptNm == null) {
			tfDeptNm = new JTextField();
			tfDeptNm.setColumns(10);
			tfDeptNm.setBounds(14, 304, 116, 21);
		}
		return tfDeptNm;
	}
	
	private JLabel getLabel_3() {
		if (label_3 == null) {
			label_3 = new JLabel("* 선택 입력 *");
			label_3.setFont(new Font("굴림", Font.BOLD, 14));
			label_3.setBounds(194, 246, 96, 15);
		}
		return label_3;
	}
	
	private JLabel getLabel_4() {
		if (label_4 == null) {
			label_4 = new JLabel("Location ID :");
			label_4.setBounds(192, 276, 86, 15);
		}
		return label_4;
	}
	
	private JLabel getLabel_5() {
		if (label_5 == null) {
			label_5 = new JLabel("Manager ID :");
			label_5.setBounds(190, 307, 86, 15);
		}
		return label_5;
	}
	
	private JTextField getTfMnageId() {
		if (tfMnageId == null) {
			tfMnageId = new JTextField();
			tfMnageId.setColumns(10);
			tfMnageId.setBounds(275, 304, 216, 21);
		}
		return tfMnageId;
	}
	
	private JTextField getTfLcId() {
		if (tfLcId == null) {
			tfLcId = new JTextField();
			tfLcId.setColumns(10);
			tfLcId.setBounds(275, 273, 216, 21);
		}
		return tfLcId;
	}
	
	private void deptSelect() {
		try {
			connect(); // DB 연결을 한다.

			String deptInfo = "";
			// 콤보 박스에서 선택한 정보를 String 변수로 저장 한다.
			String selectedItem = cbSearchDept.getSelectedItem().toString();

			if (selectedItem.equals("ID")
					&& !tfDeptInfo.getText().trim().equals("")) {
				deptInfo = " AND A.DEPARTMENT_ID LIKE '%"
						+ tfDeptInfo.getText().trim() + "%' ";
			}

			if (selectedItem.equals("Name")
					&& !tfDeptInfo.getText().trim().equals("")) {
				deptInfo = " AND A.DEPARTMENT_NAME LIKE '%"
						+ tfDeptInfo.getText().trim() + "%' ";
			}

			String qry = "SELECT A.DEPARTMENT_ID, A.DEPARTMENT_NAME, A.MANAGER_ID, "
					+ " 		 B.FIRST_NAME, B.LAST_NAME, A.LOCATION_ID "
					+ "  FROM DEPTS A, EMP B "
					+ " WHERE A.DEPARTMENT_ID = B.DEPARTMENT_ID(+) "
					+ "   AND A.MANAGER_ID = B.EMPLOYEE_ID(+) "
					+ deptInfo
					+ "	 AND A.DEPARTMENT_ID != 0 "
					+ " GROUP BY A.DEPARTMENT_ID, A.DEPARTMENT_NAME, A.MANAGER_ID, "
					+ "			B.FIRST_NAME, B.LAST_NAME, A.LOCATION_ID "
					+ " ORDER BY DEPARTMENT_ID ";

			pstmt = conn.prepareStatement(qry);
			result = pstmt.executeQuery();
			vector = new Vector<MemberBean>();

			while (result.next()) {
				MemberBean m = new MemberBean();

				m.setDeptId(result.getInt("department_id"));
				m.setDeptName(result.getString("department_name"));
				m.setFirName(result.getString("first_name"));
				m.setLastName(result.getString("last_name"));
				m.setMngId(result.getInt("manager_id"));
				m.setLocationId(result.getInt("location_id"));

				vector.add(m);
			}

			result.close();
			pstmt.close();
			close();

			dtm = new DefaultTableModel();
			dtm.setColumnIdentifiers(new String[] { "Dept ID", "Dept Name",
					"Last Name", "First Name", "Manager ID" });

			for (MemberBean m : vector) {
				dtm.addRow(new Object[] { m.getDeptId(), m.getDeptName(),
						m.getLastName(), m.getFirName(), m.getMngId() });
			}

			table.setModel(dtm);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	// 부서의 department_id를 0으로 변경하는 메소드
	private void deptUpdate() {
	
		try {
			connect();

			// 부서철회

			String qry = "UPDATE DEPTS SET  DEPARTMENT_NAME = ? , MANAGER_ID = ?, LOCATION_ID = ?   WHERE DEPARTMENT_ID = ?  " ;
			// 부서수정
			pstmt = conn.prepareStatement(qry);
			
			pstmt.setString(1, tfDeptNm.getText());

			pstmt.setInt(2, Integer.parseInt(tfMnageId.getText()));
			
			pstmt.setInt(3, Integer.parseInt(tfLcId.getText()));
			
			pstmt.setInt(4, Integer.parseInt(tfDeptId.getText()));
			
			pstmt.executeUpdate();
			
			pstmt.close();
			System.out.println("여기는 어때?");
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		/*try {
			connect();
			
			String qry = "UPDATE EMP SET DEPARTMENT_ID = 0 WHERE DEPARTMENT_ID = " + selDeptId;
			
			pstmt = conn.prepareStatement(qry);
			pstmt.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				pstmt.close();
				close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}*/
	}
	
	// 해당 부서를 삭제
	private void deptDelete() {
		try {
			connect();
			
			String qry = "DELETE FROM DEPTS WHERE DEPARTMENT_ID = " + selDeptId;
			pstmt = conn.prepareStatement(qry);
			pstmt.executeUpdate();
			
			JOptionPane.showMessageDialog( this, "선택하신 부서가 삭제 되었습니다.", "부서 삭제", 
					JOptionPane.INFORMATION_MESSAGE);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				pstmt.close();
				close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
	}
}
